import { list as datasourceList, scheduleList as list, scheduleDetail as detail, importData } from '../services/datasource';
import { stateCreate, effectsCreate, reducers, dataHandle } from 'src/models/modelCreator';
import { systemConstants } from 'src/utils/constants';
import { basicInfo as indexDetail } from '../services/index';
import { detail as appDetail } from '../services/app';

export default {
    namespace: 'control',
    state: {
      ...stateCreate(),
      moduleName: 'control',
      controlType: '0',
      relatedData: {
        appData: {},
        datasourceList: [],
        indexData: {}
      },
      datasourceSelectStyle: {
        width: 200,
        visibility: 'hidden'
      },
      importedDatasource: null,
      searchParams: {
        pageSize: systemConstants.pageSize,
        pageNo: 1,
        beginDate: systemConstants.range[0].format(systemConstants.dateFormat.backEnd),
        endDate: systemConstants.range[1].format(systemConstants.dateFormat.backEnd),
      }
    },
    subscriptions: {
      setup({ history, dispatch }) {
        // 监听 history 变化，当进入 `/` 时触发 `load` action
        return history.listen(({ pathname, query }) => {
          dispatch({
            type: 'paramsChange',
            payload: query
          });

          if (pathname.match(/\/control/)) {
            dispatch({
              type: 'relatedData',
              payload: { module: 'control' }
            });
          }
        });
      },
    },
    effects: {
      ...effectsCreate({ list, detail }, dataHandle),

      * doImport({ payload, }, { call, put, select }) {
        const { data, error } = yield call(importData, payload);
        if (data) {
          yield put({
            type: 'list',
            payload: { module: 'control' }
          });
        } else if (error && error.fieldErrors) {
          yield put({
            type: 'putError',
            payload: {
              fieldErrors: error.fieldErrors,
            }
          });
        }
      },

      * relatedData({ payload, }, { call, put, select }) {
        const {projectId, indexId} = yield select(state => state[payload.module]['searchParams']);

        const appDetailReq = call(appDetail, projectId);
        const indexDetailReq = call(indexDetail, indexId);
        const datasourceListReq = call(datasourceList, {indexId, projectId});

        const appResp = yield appDetailReq;
        const indexResp = yield indexDetailReq;
        const datasourceListResp = yield datasourceListReq;

        const { data, error } = {
          data: { appData: appResp.data, indexData: indexResp.data, datasourceList: datasourceListResp.data },
          error: appResp.error || indexResp.error || datasourceListResp.error
        };
        yield dataHandle.relatedDataHandle({ data, error }, { put });
      },
    },

    reducers: {
      ...reducers,
      controlTypeChange(state, action) {
        let datasourceSelectStyle = { ...state.datasourceSelectStyle };
        datasourceSelectStyle.visibility = action.payload === '0' ? 'hidden' : 'visible';

        return {
          ...state,
          datasourceSelectStyle,
          controlType: action.payload
        };
      },
      datasourceChange(state, action) {
        return {
          ...state,
          importedDatasource: action.payload
        };
      },

      relatedDataSuccess(state, action) {
        const {appData, indexData, datasourceList } = action.payload;
        return {
          ...state,
          relatedData: { ...action.payload },
        };
      },
    }
}
